package com.custom.base.base

import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.custom.base.R
import com.custom.base.type.AnimationActivityType
import com.custom.base.util.IntentUtil

/**
 * 作者：
 * 时间：2020/5/11
 * 描述：
 **/
abstract class BaseAdapter<T>(listModel: MutableList<T>, activity: Activity?) :
    BaseSDAdapter<T>(listModel, activity) {
    private var mToast: Toast? = null
    private val mHandler = Handler(Looper.getMainLooper())

    /**
     * show Toast
     */
    private val mShowToastRunnable = Runnable { mToast?.show() }

    override fun onGetView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
        if (convertView == null) {
            val layoutId = getLayoutId(position, null, parent)
            convertView = mInflater.inflate(layoutId, parent, false)
            //对于listview，注意添加这一行，即可在item上使用高度
            com.custom.base.util.AutoUtils.autoSize(convertView)
        }
        bindData(position, convertView, parent, mListModel[position])
        return convertView
    }

    abstract fun getLayoutId(position: Int, convertView: View?, parent: ViewGroup?): Int

    abstract fun bindData(position: Int, convertView: View?, parent: ViewGroup?, model: T)

    /**
     * 简化Toast
     */
    protected fun showToastLooper(message: String) {
        Looper.prepare()
        showToast(message)
        Looper.loop()
    }

    /**
     * 简化Toast
     */
    protected fun showToast(messageId: String) {
        if (mToast == null) {
            mToast = Toast.makeText(mActivity, messageId, Toast.LENGTH_SHORT)
        }
        mToast?.setText(messageId)
        mToast?.cancel()
        mHandler.removeCallbacks(mShowToastRunnable)
        mHandler.postDelayed(mShowToastRunnable, 0)
    }

    /**
     * 页面跳转
     */
    fun startActivity(
        clz: Class<*>,
        bundle: Bundle? = null,
        view:View? = null,
        requestCode: Int = 0,
        flags: List<Int> = listOf(),
        animationStartType: AnimationActivityType = AnimationActivityType.RIGHT,
        animationEndType:Int = R.anim.animo_no
    ) {
        IntentUtil.startActivity(clz, bundle, requestCode, flags, view,animationStartType = animationStartType,animationEndType = animationEndType)
    }
}
